//
//  MJRefresh+Extention.swift
//  AJiTai
//
//  Created by liufengting on 2020/5/19.
//  Copyright © 2020 BO ZHOU. All rights reserved.
//

import UIKit
import MJRefresh

public extension UIScrollView {

    func refreshHeaderWith(block: @escaping MJRefreshComponentAction) {
        let header = MJRefreshNormalHeader(refreshingBlock: block)
        header.lastUpdatedTimeLabel?.isHidden = true
        header.stateLabel?.isHidden = false
        header.isAutomaticallyChangeAlpha = true
        self.mj_header = header
    }
    
    func loadMoreFooterWith(block: @escaping MJRefreshComponentAction) {
        let footer = MJRefreshAutoNormalFooter(refreshingBlock: block)
        footer.isRefreshingTitleHidden = true
        footer.stateLabel?.isHidden = false
        footer.isAutomaticallyChangeAlpha = true
        self.mj_footer = footer;
    }
    
    func headerBeginRefreshing() {
        self.mj_header?.beginRefreshing()
    }
    
    func headerEndRefreshing() {
        self.mj_header?.endRefreshing()
    }
    
    func footerBeginRefreshing() {
        self.mj_footer?.beginRefreshing()
    }
    
    func footerEndRefreshing() {
        self.mj_footer?.endRefreshing()
    }
    
    func endRefreshing() {
        if self.mj_header?.isRefreshing ?? false {
            self.headerEndRefreshing()
        }
        if self.mj_footer?.isRefreshing ?? false {
            self.footerEndRefreshing()
        }
    }

}
